﻿using System;
using System.Collections;

namespace Utilities
{
	public static class ListExtension
	{
		public static void SwapElements(this IList source, int index1, int index2)
		{
				var tmp = source[index1];
				source[index1] = source[index2];
				source[index2] = tmp;
		}
		public static void MoveElementToBegin(this IList source, int index)
		{
			try
			{
				var element = source[index];
				source.RemoveAt(index);
				source.Insert(0, element);
			}
			catch(IndexOutOfRangeException)
			{
				throw new ArgumentOutOfRangeException("index");
			}
		}
		public static void MoveElementToBegin(this IList source, object item)
		{
			var currentIndex = source.IndexOf(item);
			if (currentIndex >= 0)
			{
				source.RemoveAt(currentIndex);
				source.Insert(0, item);
			}
		}

		public static void MoveElementToEnd(this IList source, object item)
		{
			var currentIndex = source.IndexOf(item);
			if (currentIndex >= 0)
			{
				source.RemoveAt(currentIndex);
				source.Insert(source.Count == 0 ? 0 : source.Count - 1, item);
			}
		}
	}
}
